import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:mortgageCalculator/modal/api.dart';

class ResultPage extends StatefulWidget {
  ResultPage({Key key, this.result}) : super(key: key);
  Map result = {};

  @override
  _ResultPageState createState() => _ResultPageState();
}

class _ResultPageState extends State<ResultPage> {
  TextEditingController addressController = TextEditingController();

  Map debxContent = {'Interest': '-1'};
  Map debjContent = {};

  @override
  void initState() {
    getResult(widget.result['dkje'], widget.result['dkys'],
            double.parse(widget.result['dklv']) / 100, 0)
        .then((res) {
      setState(() {
        debxContent = res['d'][0];
        debjContent = res['d'][1];
      });
      print(debxContent);
    });
    super.initState();
  }

  renderList(obj) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: 50,
      margin: EdgeInsets.only(bottom: 15),
      decoration: new BoxDecoration(
        //设置四周圆角 角度
        borderRadius: BorderRadius.all(Radius.circular(4.0)),
        //设置四周边框
        border: new Border.all(width: 1, color: Colors.orangeAccent),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            width: 100,
            height: 50,
            color: Colors.amber,
            child: Center(
              child: Text(obj['label'],
                  style: TextStyle(
                      fontSize: 16, //设置字体大小
                      fontWeight: FontWeight.w500)),
            ),
          ),
          Expanded(
            child: Container(
              padding: EdgeInsets.only(left: 25),
              child: Text(obj['content'],
                  style: TextStyle(
                      fontSize: 16, //设置字体大小
                      fontWeight: FontWeight.w500)),
            ),
          ),
          obj['label'] == '还款特点'
              ? Container()
              : Container(
                  width: 80,
                  height: 50,
                  color: Colors.amber,
                  child: Center(
                      child: Text(obj['danwei'],
                          style: TextStyle(
                              fontSize: 16, //设置字体大小
                              fontWeight: FontWeight.w500))),
                ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xFF39AC76),
          title: Text('计算结果'),
        ),
        body: debxContent['Interest'] == '-1'
            ? Container(
                child: Center(
                    child: CircularProgressIndicator(
                        backgroundColor: Colors.grey[300],
                        valueColor: new AlwaysStoppedAnimation<Color>(
                            Color(0xFF39AC76)))),
              )
            : SingleChildScrollView(
                padding: EdgeInsets.all(15),
                child: Column(
                  children: [
                    Text('等额本息还款方式计算结果：',
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.w500,
                        )),
                    SizedBox(height: 15),
                    renderList({
                      'label': '贷款总额',
                      'content': debxContent['Loan'].toString(),
                      'danwei': '元',
                    }),
                    renderList({
                      'label': '还款月数',
                      'content': debxContent['Months'].toString(),
                      'danwei': '月',
                    }),
                    renderList({
                      'label': '每月还款',
                      'content': debxContent['Payment'].toString(),
                      'danwei': '元',
                    }),
                    renderList({
                      'label': '还款特点',
                      'content': '等额本息每月还款额度相等',
                      'danwei': '',
                    }),
                    renderList({
                      'label': '利息总额',
                      'content': debxContent['Interest'].toString(),
                      'danwei': '元',
                    }),
                    renderList({
                      'label': '本息合计',
                      'content': debxContent['Total'].toString(),
                      'danwei': '元',
                    }),
                    Text('等额本金还款方式计算结果：',
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.w500,
                        )),
                    SizedBox(height: 15),
                    renderList({
                      'label': '贷款总额',
                      'content': debjContent['Loan'].toString(),
                      'danwei': '元',
                    }),
                    renderList({
                      'label': '还款月数',
                      'content': debjContent['Months'].toString(),
                      'danwei': '月',
                    }),
                    renderList({
                      'label': '每月还款',
                      'content': debjContent['Payment'].toString(),
                      'danwei': '元',
                    }),
                    renderList({
                      'label': '每月递减',
                      'content': debjContent['Decline'].toString(),
                      'danwei': '元',
                    }),
                    renderList({
                      'label': '利息总额',
                      'content': debjContent['Interest'].toString(),
                      'danwei': '元',
                    }),
                    renderList({
                      'label': '本息合计',
                      'content': debjContent['Total'].toString(),
                      'danwei': '元',
                    }),
                  ],
                ),
              ));
  }
}
